use std::task::{Context, Waker};
use std::time::SystemTime;
use reqwest;
use reqwest::{Client, multipart, RequestBuilder, StatusCode};
use reqwest::header::{HeaderMap, HeaderValue};
use reqwest::multipart::Part;
use serde::Serialize;
use tokio::fs::File;
use tokio::io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter};
use crate::ldate_time::date_utils;
use futures_util::StreamExt;
pub async fn send_http() {
    let client: Client = reqwest::Client::new();
    let mut request_builder: RequestBuilder =
        client.get("")
            .timeout(core::time::Duration::from_secs(10));

    // 设置header
    let mut map = HeaderMap::new();
    map.insert("timestamp", HeaderValue::from(date_utils::get_now_stamp()));
    map.insert("Content-Type", HeaderValue::from_static("application/json"));
    let request_builder = request_builder.headers(map);

    let param = GetParam { name: String::from("xiao ming") };

    // file 传输
    // let mut file = File::open("'").await.unwrap();
    // let read = BufReader::new(file);
    // let mut multipart = multipart::Form::new();
    // multipart = multipart.text("name", "hua");
    // let bd = reqwest::Body::wrap_stream(read);
    // // multipart::Part::bytes(buffer);
    // multipart = multipart.part("file",Part::stream(bd));
    // request_builder.multipart(multipart);


    // 设置 query
    // let yz: [(&str,&str); 2] = [("", ""),("","")];
    // req_build01.query(&param);

    // 设置 form 表单
    // req_build01.form(&param);

    // 设置 body
    // req_build01.body(reqwest::Body::from(""));

    // 设置 json
    // req_build01.json(&param);


    let resp = request_builder.send().await.unwrap();
   let total = resp.content_length().unwrap_or(0);

    // let data = resp.text().await;
    // let staus = resp.status();

    // 下载文件
    // let  file = File::create("").await?;
    // let mut write_buf = BufWriter::new(file);
    //
    // let mut bytes_steam = request_builder.send().await.unwrap().bytes_stream();
    // let mut downloaded = 0;
    //
    // while let Some(item) = bytes_steam.next().await {
    //     match item {
    //         Ok(chunk) => {
    //             downloaded += chunk.len();
    //             write_buf.write(chunk.iter().as_slice()).await.expect("");
    //         },
    //         Err(_e) => {}
    //     }
    // }
    // write_buf.flush().await.expect("");

}


pub async fn get_school_test() {
    let resp = Client::new().get("https://sdt.cuopen.net/rest2/client/product")
        .timeout(core::time::Duration::from_secs(10))
        .header("Content-Type", "application/json")
        .query(&[("deviceId","test001"),("productId","128909902")])
        .send().await.unwrap().text().await;

    println!("打印 response: {:?}", resp);
}


#[derive(Serialize)]
struct GetParam {
    name: String,
}

#[derive(Serialize)]
struct KONG {}




pub fn test_http() {
  tokio::runtime::Runtime::new().unwrap().block_on(async {
      get_school_test().await;
  });

}












